package com.hmdp.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.hmdp.utils.RedisConstants.LOGIN_USER_KEY;
import static com.hmdp.utils.RedisConstants.LOGIN_USER_TTL;

/**
 * 用户登录信息缓存 拦截器
 */

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 拦截器链 负责用户信息缓存 当前面拦截器拦截到用户请求，会把用户放入到ThreadLocal
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从ThreadLocal 中获取用户
        UserDTO user = UserHolder.getUser();
        // ThreadLocal 中无用户 拦截
        if (user == null) {
            return false;
        }
        return true;
    }

}
